<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2019/3/19
 * Time: 11:38
 */

namespace common\sources\write\taskorder;

use common\models\craftsman\TaskOrder;
use Yii;
class TaskOrderAccess implements ITaskOrderAccess
{
    public function addTaskOrder($param)
    {
        return Yii::$app->db->createCommand()->insert('task_order', [
            'plat_type' =>  $param['plat_type'],
            'buy_id' => $param['buy_id'],
            'task_type' => $param['task_type'],
            'tbname'=>$param['tbname'],
            'task_commod_id' => $param['task_commod_id'],
            'main_url1' => $param['main_url1'],
            'main_url2' => $param['main_url2'],
            'title' => $param['title'],
            'colour' => $param['colour'],
            'price' => $param['price'],
            'num' => $param['num'],
            'size' => $param['size'],
            'ship_addr' => $param['ship_addr'],
            'shop_name' => $param['shop_name'],
            'port_type' => $param['port_type'],
            'task_id' => $param['task_id'],
            'order_status' => 1,
            'advance_amount' => $param['advance_amount'],
            'commission' => $param['commission'],
            'receive_name' => $param['receive_name'],
            'receive_phone' => $param['receive_phone'],
            'return_type' => $param['return_type'],
            'pro_name' =>$param['pro_name'],
            'pro_code' => $param['pro_code'],
            'city_name' => $param['city_name'],
            'city_code' => $param['city_code'],
            'area_name' => $param['area_name'],
            'area_code' => $param['area_code'],
            'addr' => $param['addr'],
            'bus_id' => $param['bus_id'],
            'keywords' => $param['keywords'],
            'task_order_num' => $param['task_order_num'],
            'range_price' => $param['range_price'],
            'ww_name' => $param['ww_name'],
            'actual_price' => $param['actual_price'],
            'qr_code_url' => $param['qr_code_url'],
            'task_shop_image_id' => isset($param['task_shop_image_id']) ? $param['task_shop_image_id'] : '0',
            'praise_keywords' => $param['praise_keywords'],
            'shop_id' => $param['shop_id'],
            'express_type' => $param['express_type'],
            'task_commod_keyword_id' => $param['task_commod_keyword_id'],
            'praise_keywords_id' => isset($param['praise_keywords_id']) ? $param['praise_keywords_id'] : 0,
            'tb_id' => $param['tb_id'],
            'create_time' => time(),
        ])->execute();
    }
    //买手上传进店关键字截图
    public function uploadKeywordImg($data)
    {
        $sql = 'UPDATE ' . TaskOrder::tableName() . ' SET keyword_url = :keyword_url,task_shop_status = :task_shop_status,update_time = :update_time,search_time = :search_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':keyword_url' =>$data['keyword_url'],
                ':task_shop_status' => $data['task_shop_status'],
                ':update_time' => time(),
                ':search_time' => time(),
                ':id' => $data['id']
            ])->execute();
    }
    //浏览子订单
    public function browseTask($data)
    {
        $sql = 'UPDATE ' . TaskOrder::tableName() . ' SET browse_time = :browse_time,task_shop_status = :task_shop_status,update_time = :update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':browse_time' => time(),
                ':task_shop_status' => $data['task_shop_status'],
                ':update_time' => time(),
                ':id' => $data['id']
            ])->execute();
    }
    public function submitTask($data)
    {
        $sql = 'UPDATE ' . TaskOrder::tableName() . ' SET order_status = :order_status,task_shop_status = :task_shop_status,order_time = :order_time,actual_price = :actual_price,purchase_num = :purchase_num,
                                                    advance_amount = :advance_amount,actual_refunds = :actual_refunds,pay_order_num = :pay_order_num,addr_url = :addr_url,refund_amount = :refund_amount,
                                                    pay_url = :pay_url,update_time = :update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':order_status' => $data['order_status'],
                ':task_shop_status'=> $data['task_shop_status'],
                ':order_time' => time(),
                ':actual_price' => $data['actual_price'],
                ':purchase_num' => $data['purchase_num'],
                ':advance_amount' => $data['advance_amount'],
                ':actual_refunds' => $data['actual_refunds'],
                ':pay_order_num' => $data['pay_order_num'],
                ':addr_url' => $data['addr_url'],
                ':refund_amount' => $data['refund_amount'],
                ':pay_url' => $data['pay_url'],
                ':update_time' => time(),
                ':id' => $data['id']
            ])->execute();
    }
    public function modifyTask($data)
    {
        $sql = 'UPDATE ' . TaskOrder::tableName() . ' SET order_status = :order_status,give_up_time = :give_up_time,update_time = :update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':order_status' => $data['order_status'],
                ':give_up_time' => time(),
                ':update_time' => time(),
                ':id' => $data['id']
            ])->execute();
    }
    public function confirmTask($data)
    {
        $sql = 'UPDATE ' . TaskOrder::tableName() . ' SET order_status = :order_status,favorable_time = :favorable_time,update_time = :update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':order_status' => $data['order_status'],
                ':favorable_time' => time(),
                ':update_time' => time(),
                ':id' => $data['id']
            ])->execute();
    }
    //买手确认返款
    public function confirmRefunds($data)
    {
        $sql = 'UPDATE ' . TaskOrder::tableName() . ' SET order_status = :order_status,confirm_time = :confirm_time,update_time = :update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':order_status' => $data['order_status'],
                ':confirm_time' => time(),
                ':update_time' => time(),
                ':id' => $data['id']
            ])->execute();
    }
    //驳回返款
    public function rejectRefunds($data)
    {
        $sql = 'UPDATE ' . TaskOrder::tableName() . ' SET order_status = :order_status,reject_time = :reject_time,actual_refunds = :actual_refunds,actual_price = :actual_price,refund_amount = :refund_amount,advance_amount = :advance_amount,update_time = :update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':order_status' => $data['order_status'],
                ':actual_refunds' => $data['actual_refunds'],
                ':actual_price' => $data['actual_refunds'],
                ':refund_amount' => $data['actual_refunds'],
                ':advance_amount' => $data['actual_refunds'],
                ':reject_time' => time(),
                ':update_time' => time(),
                ':id' => $data['id']
            ])->execute();
    }
    //上传好评图片
    public function uploadPositive($data)
    {
        $sql = 'UPDATE ' . TaskOrder::tableName() . ' SET order_status = :order_status,parise_url = :parise_url,tao_parise_url = :tao_parise_url,favorable_time =:favorable_time,update_time = :update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':order_status' => $data['order_status'],
                ':parise_url' => $data['parise_url'],
                ':tao_parise_url' => $data['tao_parise_url'],
                ':favorable_time' => time(),
                ':update_time' => time(),
                ':id' => $data['id']
            ])->execute();
    }
    //核对链接
    public function checkUrl($task_order_id)
    {
        $sql = 'UPDATE ' . TaskOrder::tableName() . ' SET is_check = :is_check,update_time = :update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':is_check' => 1,
                ':update_time' => time(),
                ':id' => $task_order_id
            ])->execute();
    }
    //第一步操作
    public function modifyTaskOrder($data)
    {
        $sql = 'UPDATE ' . TaskOrder::tableName() . ' SET task_shop_status = :task_shop_status,update_time = :update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':task_shop_status' => $data['task_shop_status'],
                ':update_time' => time(),
                ':id' => $data['id']
            ])->execute();
    }
    //申请提现
    public function modifyTaskOrderApply($data)
    {
        return TaskOrder::updateAll(["order_status" => $data['order_status'],"apply_cash_time" =>time()],["id" => $data['task_order_ids']]);
    }
    //撤销提现
    public function modifyTaskOrderRevoke($data)
    {
        return TaskOrder::updateAll(["order_status" => $data['order_status']],["id" => $data['task_order_arr']]);
    }
}